package com.xuliugen.jwt.demo.controller.base;

import com.xuliugen.jwt.demo.exception.RequestBodyValidException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;

import javax.servlet.http.HttpServletResponse;

/**
 * @author xuliugen
 * @date 2017/12/5
 */
public class BaseController {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 处理请求体参数异常
     * @param bindingResult
     */
    protected void bindingResultWrapper(BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new RequestBodyValidException("请求体参数错误！", bindingResult);
        }
    }

    /**
     * 判断对象是为空
     * @param obj
     * @param response
     */
    public void resultIsNull(Object obj, HttpServletResponse response) {
        if (obj == null) {
            response.setStatus(HttpStatus.NO_CONTENT.value());
        }
    }
}
